// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package com.myplans.model;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;

import com.myplans.criteria.UserSearchCriteria;
import com.myplans.vo.PlanValue;

/**
 * @see com.myplans.model.Plan
 */
public class PlanDaoImpl
    extends com.myplans.model.PlanDaoBase
{
	/**
	 * @see com.myplans.model.PlanDao#toPlanValue(com.myplans.model.Plan, com.myplans.vo.PlanValue)
	 */
	public void toPlanValue(
			com.myplans.model.Plan source,
			com.myplans.vo.PlanValue target)
	{
		// @todo verify behavior of toPlanValue
		super.toPlanValue(source, target);
		
		if (source.getUser() != null) {
        	target.setUser(getUserDao().
        			toUserValue(source.getUser()));
        }
		if (source.getCategory() != null) {
			target.setCategory(getCategoryDao().
					toCategoryValue(source.getCategory()));
		}
		if (source.getDate() != null) {
	        Calendar c = Calendar.getInstance();
	        c.setTimeInMillis(source.getDate().getTime());
	        target.setDate(c);
	    }
	    UserSearchCriteria criteria = new UserSearchCriteria();
	    criteria.setPlan(target);
		target.setParticipants(getUserDao().getUsers(criteria)); 
	}
	
	
	/**
	 * @see com.myplans.model.PlanDao#toPlanValue(com.myplans.model.Plan)
	 */
	public com.myplans.vo.PlanValue toPlanValue(final com.myplans.model.Plan entity)
	{
		// @todo verify behavior of toPlanValue
		return super.toPlanValue(entity);
	}
	
	
	/**
	 * Retrieves the entity object that is associated with the specified value object
	 * from the object store. If no such entity object exists in the object store,
	 * a new, blank entity is created
	 */
	private com.myplans.model.Plan loadPlanFromPlanValue(com.myplans.vo.PlanValue planValue)
	{
		Plan plan = Plan.Factory.newInstance();
    	if (planValue.getId() != null) {
    		plan = this.load(planValue.getId());
    	}
        return plan; 
	}
	
	
	/**
	 * @see com.myplans.model.PlanDao#planValueToEntity(com.myplans.vo.PlanValue)
	 */
	public com.myplans.model.Plan planValueToEntity(com.myplans.vo.PlanValue planValue)
	{
		// @todo verify behavior of planValueToEntity
		com.myplans.model.Plan entity = this.loadPlanFromPlanValue(planValue);
		this.planValueToEntity(planValue, entity, true);
		return entity;
	}
	
	
	/**
	 * @see com.myplans.model.PlanDao#planValueToEntity(com.myplans.vo.PlanValue, com.myplans.model.Plan)
	 */
	public void planValueToEntity(
			com.myplans.vo.PlanValue source,
			com.myplans.model.Plan target,
			boolean copyIfNull)
	{
		// @todo verify behavior of planValueToEntity
		super.planValueToEntity(source, target, copyIfNull);
		
		if (source.getUser() != null) {
        	target.setUser(getUserDao().userValueToEntity(source.getUser()));
        }
		if (source.getCategory() != null) {
			target.setCategory(getCategoryDao().
					categoryValueToEntity(source.getCategory()));
		}
		if (source.getDate() != null) {
        	Timestamp t = new Timestamp(Calendar.getInstance().getTimeInMillis());
    		t.setTime(source.getDate().getTimeInMillis());
    		target.setDate(t);
        }
	}
	
    /**
     * @see com.myplans.model.PlanDao#getPlans(com.myplans.criteria.PlanSearchCriteria)
     */
    @SuppressWarnings( {"unchecked", "rawtypes"} )
	protected java.util.List<com.myplans.vo.PlanValue> handleGetPlans(com.myplans.criteria.PlanSearchCriteria searchCriteria)
    {
        Criteria criteria = getSession().createCriteria(Plan.class);
        Disjunction disjunction = Restrictions.disjunction();
        
        if (searchCriteria.getCategory() != null) {
        	Criteria categoryCriteria = criteria.createAlias("category", "cat");
        	disjunction.add(Restrictions.ilike("cat.name", "%" + searchCriteria.getCategory() + "%"));
        }
        if (searchCriteria.getName() != null) {
        	disjunction.add(Restrictions.ilike("name", "%" + searchCriteria.getName() + "%"));
        }
        if (searchCriteria.getLocation() != null) {
        	disjunction.add(Restrictions.ilike("location", "%" + searchCriteria.getLocation() + "%"));
        }
        if (searchCriteria.getDescription() != null) {
        	disjunction.add(Restrictions.ilike("description", "%" + searchCriteria.getDescription() + "%"));
        }
        
        criteria.add(disjunction);
        
		List result = criteria.list();
        toPlanValueCollection(result);
        return (List<PlanValue>) result;
    }


}